package com.pcj.wechat.controller;

import com.pcj.pojo.User;
import com.pcj.service.UserService;
import com.pcj.utils.JsonResult;
import com.pcj.utils.MD5Util;
import com.pcj.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: wechat
 * @author: 作者
 * @create: 2022-03-03 10:50
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/registerOrLogin")
    @ResponseBody
    //用户登录与注册一体化
    public JsonResult registerOrLogin(User user) {
        User uResult = userService.selectByUsername(user.getUsername());
        UserVo userVo = new UserVo();
        if (uResult != null) {
            if (!MD5Util.code(user.getPassword()).equals(uResult.getPassword())) {
                return JsonResult.errorMsg("登录密码错误");
            }
            BeanUtils.copyProperties(uResult, userVo);
            return JsonResult.success(userVo);
        } else {  //用户不存在  注册
            user.setNickname("测试");
            user.setQrcode("erweima");
            user.setPassword(MD5Util.code(user.getPassword()));
            user.setFaceImage("");
            user.setFaceImageBig("");
            uResult = userService.insert(user);
        }
        //防止泄露敏感信息

        BeanUtils.copyProperties(uResult, userVo);
        System.out.println(userVo);
        return JsonResult.success(userVo);
    }

    @RequestMapping("/setNickname")
    @ResponseBody
    //修改昵称
    public JsonResult setNickname(User user) {
        User userResult = userService.updateUserInfo(user);
        return JsonResult.success(userResult);
    }

    @RequestMapping("/getuser")
    public String getUserById(String id, Model model) {
        User user = userService.getUserById(id);
        System.out.println(user);
        model.addAttribute("user", user);
        return "test";
    }

}
